package com.sgcc.common.core.utils;

import com.sgcc.common.core.domain.R;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HttpClientUtil
 * @author jingqian
 * @create 2021/3/31
 */

public class HttpClientUtil {
    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    /**
     * @Title: doGet
     * @Description: get方式
     * @param url 请求路径
     * @param params 参数
     */
    public static String doGet(String url, Map<String, String> params) {

        // 返回结果
        String result = "";
        // 创建HttpClient对象
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = null;
        try {
            // 拼接参数,可以用URIBuilder,也可以直接拼接在？传值，拼在url后面，如下--httpGet = new
            // HttpGet(uri+"?id=123");
            URIBuilder uriBuilder = new URIBuilder(url);
            if (null != params && !params.isEmpty()) {
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    uriBuilder.addParameter(entry.getKey(), entry.getValue());
                    // 或者用
                    // 顺便说一下不同(setParameter会覆盖同名参数的值，addParameter则不会)
                    // uriBuilder.setParameter(entry.getKey(), entry.getValue());
                }
            }
            URI uri = uriBuilder.build();
            // 创建get请求
            httpGet = new HttpGet(uri);
            logger.info("访问路径：" + uri);
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 返回200，请求成功
                // 结果返回
                result = EntityUtils.toString(response.getEntity());
                logger.info("请求成功！，返回数据：" + result);
            } else {
                logger.info("请求失败！");
            }
        } catch (Exception e) {
            logger.info("请求失败!");
            logger.error(ExceptionUtils.getStackTrace(e));
        } finally {
            // 释放连接
            if (null != httpGet) {
                httpGet.releaseConnection();
            }
        }
        return result;
    }

    /**
     * @Title: doPost
     * @Description: post请求
     * @param url
     * @param params
     * @return
     */
    public static String doPost(String url, Map<String, String> params) {
        String result = "";
        // 创建httpclient对象
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        try { // 参数键值对
            if (null != params && !params.isEmpty()) {
                List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                NameValuePair pair = null;
                for (String key : params.keySet()) {
                    pair = new BasicNameValuePair(key, params.get(key));
                    pairs.add(pair);
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
                logger.info("返回数据：>>>" + result);
            } else {
                logger.info("请求失败！，url:" + url);
            }
        } catch (Exception e) {
            logger.error("请求失败");
            logger.error(ExceptionUtils.getStackTrace(e));
            e.printStackTrace();
        } finally {
            if (null != httpPost) {
                // 释放连接
                httpPost.releaseConnection();
            }
        }
        return result;
    }

    /**
     * @Title: sendJsonStr
     * @Description: post发送json字符串
     * @param url
     * @param params
     * @return 返回数据
     */
    public static R<String> sendJsonStr(String url, String params) {
        String result = "";

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        try {
            logger.info("请求地址："+url);
            logger.info("请求参数："+params);
            httpPost.addHeader("Content-type", "application/json; charset=utf-8");
            httpPost.setHeader("Accept", "application/json");
            if (StringUtils.isNotBlank(params)) {
                httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity());
                logger.info("返回数据：" + result);
            } else {
                logger.info("请求失败");
                return R.fail("请求失败");
            }
        } catch (IOException e) {
            logger.info("请求异常"+ExceptionUtils.getStackTrace(e));
            return R.fail("请求异常"+ExceptionUtils.getStackTrace(e));
        }
        return R.ok(result,"请求成功");
    }

    public static void main(String[] args) {
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("id", UUID.randomUUID().toString());
//        String get = doGet("http://localhost:8080/mundo/test", map);
//        System.out.println("get请求调用成功，返回数据是：" + get);
//        String post = doPost("http://localhost:8080/mundo/test", map);
//        System.out.println("post调用成功，返回数据是：" + post);
//        String json = sendJsonStr("http://localhost:8080/mundo/test", "{\"name\":\"David\"}");
//        System.out.println("json发送成功，返回数据是：" + json);

//        String result = HttpClientUtil.sendJsonStr("http://192.168.0.203:8080/web-ptm/dataService/dataRecieve", "{\n" +
//                "\t\"data\": {\n" +
//                "\t\t\"daqtime\": \"1587882997\",\n" +
//                "\t\t\"deviceID\": \"09M00000004554429\",\n" +
//                "\t\t\"deviceName\": \"2号变10千伏开关柜\",\n" +
//                "\t\t\"measurementTypeCode\": 4,\n" +
//                "\t\t\"measurementTypeName\": \"AE幅值\",\n" +
//                "\t\t\"measurementValue\": {\n" +
//                "\t\t\t\"dataFile\": \"100\\\\20200427\\\\011001_09M00000004554429_100_AESPDC35_090000.dat\",\n" +
//                "\t\t\t\"dataUnit\": 1,\n" +
//                "\t\t\t\"fre1\": -15.0,\n" +
//                "\t\t\t\"fre2\": -15.0,\n" +
//                "\t\t\t\"frequency\": 0,\n" +
//                "\t\t\t\"gain\": 0,\n" +
//                "\t\t\t\"id\": 16436267,\n" +
//                "\t\t\t\"max\": -7.0,\n" +
//                "\t\t\t\"rms\": -11.0,\n" +
//                "\t\t\t\"sampleCount\": 0,\n" +
//                "\t\t\t\"sampleRate\": 0,\n" +
//                "\t\t\t\"syncFlag\": 0,\n" +
//                "\t\t\t\"syncType\": 0\n" +
//                "\t\t},\n" +
//                "\t\t\"picURL\": \"\",\n" +
//                "\t\t\"substationID\": \"09M00000004554293\",\n" +
//                "\t\t\"substationName\": \"徐秀站\",\n" +
//                "\t\t\"testpointID\": \"10009M00000004554429100_AESPDC35\",\n" +
//                "\t\t\"testpointName\": \"超声测点1\"\n" +
//                "\t},\n" +
//                "\t\"serviceCode\": \"p_pdzzhjk_001\",\n" +
//                "\t\"systemCode\": \"web-ptm\"\n" +
//                "}");
//        System.out.println(result);

    }
}